﻿Imports RunEnv
Imports UITool
Imports System.Data.SqlClient
Public Class dlgUserMap
    Private mstrUserID As String
    Private mdtUserGroupMap As New DataTable
    Private mdtUserGroup As DataTable
    Private mdtAdapter As SqlDataAdapter


    Public Sub ShowOpen(ByVal userID As String)
        Try
            If Len(userID) <= 0 Then Exit Sub
            mstrUserID = userID
            Me.lblUserID.Text = userID
            InitDB()
            InitList()
            Me.ShowDialog()
        Catch ex As Exception
            MainException(ex)
        End Try
        
    End Sub

    Private Sub InitDB()
        Dim sql As String
        Dim insertCommand As New SqlCommand
        Dim deleteCommand As New SqlCommand
        sql = "Select * From B_UserGroupMap Where UserID ='" & mstrUserID & "'"
        mdtAdapter = New SqlDataAdapter(sql, CRunEnv.DBConnection.sqlConnection)
        mdtAdapter.Fill(mdtUserGroupMap)
        mdtUserGroupMap.PrimaryKey = {mdtUserGroupMap.Columns("UserID"), mdtUserGroupMap.Columns("UserGroupID")}
        insertCommand.Connection = CRunEnv.DBConnection.sqlConnection
        insertCommand.CommandText = "Insert Into B_UserGroupMap (UserID,UserGroupID) Values (@UserID,@UserGroupID)"
        insertCommand.Parameters.Add("@UserID", SqlDbType.NVarChar, 20, "UserID")
        insertCommand.Parameters.Add("@UserGroupID", SqlDbType.NVarChar, 50, "UserGroupID")
        mdtAdapter.InsertCommand = insertCommand

        deleteCommand.Connection = CRunEnv.DBConnection.sqlConnection
        deleteCommand.CommandText = "Delete From B_UserGroupMap Where UserID =@UserID And UserGroupID =@UserGroupID"
        deleteCommand.Parameters.Add("@UserID", SqlDbType.NVarChar, 20, "UserID")
        deleteCommand.Parameters.Add("@UserGroupID", SqlDbType.NVarChar, 50, "UserGroupID")
        mdtAdapter.DeleteCommand = deleteCommand
        sql = "Select * From B_UserGroup "
        mdtUserGroup = CRunEnv.DBConnection.GetDataTable(sql)

    End Sub

    Private Sub InitList()
        Try
            ListView1.Items.Clear()
            For Each dtRow As DataRow In mdtUserGroupMap.Rows
                ListView1.Items.Add(dtRow.Item("UserGroupID"))
            Next
        Catch ex As Exception
            MainException(ex)
        End Try
    End Sub

    Private Sub cmdAdd_Click(sender As System.Object, e As System.EventArgs) Handles cmdAdd.Click
        Dim newGroups As Collection
        Dim dlg As New dlgCommonSelect
        Try
            If mdtUserGroup.Rows.Count <= 0 Then
                MsgBox("不存在用户组！", MsgBoxStyle.Information, "系统提示")
                Exit Sub
            End If
            newGroups = dlg.ShowOpen(mdtUserGroup, "添加用户组", "UserGroupID")
            If newGroups.Count <= 0 Then Exit Sub

            Dim newRow As DataRow
            For Each newGroupID As String In newGroups
                If mdtUserGroupMap.Select("UserGroupID='" & newGroupID & "'").Length > 0 Then
                    Continue For
                End If
                newRow = mdtUserGroupMap.NewRow
                newRow.Item("UserID") = mstrUserID
                newRow.Item("UserGroupID") = newGroupID
                mdtUserGroupMap.Rows.Add(newRow)
            Next

            If mdtAdapter.Update(mdtUserGroupMap) > 0 Then
                InitList()
            End If
        Catch ex As Exception
            MainException(ex)
        End Try
    End Sub
    Private Sub cmdRemove_Click(sender As System.Object, e As System.EventArgs) Handles cmdRemove.Click
        Dim userGroupIDs As DataRow()
        Dim listViewItem1 As Windows.Forms.ListViewItem
        Dim filterStr As String = ""

        Try
            If ListView1.SelectedItems.Count <= 0 Then Exit Sub
            For Each listViewItem1 In ListView1.SelectedItems
                If Len(filterStr) = 0 Then
                    filterStr = "UserGroupID ='" & listViewItem1.Text & "'"
                Else
                    filterStr += " Or UserGroupID ='" & listViewItem1.Text & "'"
                End If
            Next

            userGroupIDs = mdtUserGroupMap.Select(filterStr)

            For Each userGroupID As DataRow In userGroupIDs
                userGroupID.Delete()
            Next

            If mdtAdapter.Update(mdtUserGroupMap) > 0 Then
                InitList()
            End If
        Catch ex As Exception
            MainException(ex)
        End Try
    End Sub
    Private Sub cmdClose_Click(sender As System.Object, e As System.EventArgs) Handles cmdClose.Click
        Me.Close()
    End Sub
End Class